﻿using System.Collections.Generic;
using System.Linq;

namespace Mvc.RazorTools.Glyphicons.CodeDesigner
{
	public class Icon
	{
		public IEnumerable<Icon> Parent { get; set; }
		public string Category { get; set; }
		public string Name { get; set; }
		public string Id { get; set; }
		public int Code { get; set; }
		public int Index { get; set; }

		public string Key
		{
			get
			{
				return string.Format("{0}.{1}.{2}", this.Category, this.Name, this.Index);
			}
		}

		public string CodeName
		{
			get
			{
				string returnValue = string.Empty;

				if (this.Index > 1)
				{
					returnValue = string.Format("{0}{1}", this.Name, this.Index);
				}
				else
				{
					var qry = from tbl in this.Parent
							  where tbl.Category == this.Category &&
							  tbl.Name == this.Name
							  select tbl;

					if (qry.Count() > 1)
					{
						returnValue = string.Format("{0}{1}", this.Name, this.Index);
					}
					else
					{
						returnValue = string.Format("{0}", this.Name);
					}
				}

				return returnValue;
			}
		}

		public override string ToString()
		{
			return string.Format("{0}/{1} [{2}]", this.Category, this.Name, this.Code);
		}
	}
}
